package com.example.constellation.Fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.example.constellation.Adapter.fg3guideradapter;
import com.example.constellation.R;
import com.example.constellation.bean.StarBean;
import com.example.constellation.utils.AssetsUtils;

import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment4 extends Fragment {
    private CircleImageView meimg;
    private TextView mytxt;
    private Map<String, Bitmap> contentlogomap;
    private List<StarBean.StarinfoBean> mdata;
    private SharedPreferences star_pref;

    //保存选择的星座位置
    int posi=0;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_4, container, false);
        meimg=view.findViewById(R.id.meimg);
        mytxt=view.findViewById(R.id.mytxt);
        contentlogomap = AssetsUtils.getContentlogomap();
        //读取共享参数中保存的星座
        String name = star_pref.getString("name", "白羊座");
        String logonamme = star_pref.getString("logoname", "baiyang");
        Bitmap bitmap = contentlogomap.get(logonamme);
        meimg.setImageBitmap(bitmap);
        mytxt.setText(name);
        meimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showdialog();
            }
        });
        return view;
    }

    private void showdialog() {
        final Dialog dialog=new Dialog(getContext());
        View medialogview=LayoutInflater.from(getContext()).inflate(R.layout.medialog,null);
        dialog.setContentView(medialogview);
        dialog.setTitle("请选择你的星座");
        GridView media_gv=medialogview.findViewById(R.id.media_gv);
        //设置适配器
        fg3guideradapter adapter=new fg3guideradapter(getContext(),mdata);
        media_gv.setAdapter(adapter);
        //dialog能否被取消
        dialog.setCancelable(true);
        //点击dialog外部能取消弹出框
        dialog.setCanceledOnTouchOutside(true);
        //dialog中的gridleview点击事件
        media_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                StarBean.StarinfoBean bean = mdata.get(position);
                String name = bean.getName();
                String logoname = bean.getLogoname();
                mytxt.setText(name);
                Bitmap bitmap = contentlogomap.get(logoname);
                meimg.setImageBitmap(bitmap);
                posi=position;//保存选择的位置

                dialog.cancel();
            }
        });
        dialog.show();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取数据
        Bundle bundle = getArguments();
        StarBean starBean= (StarBean) bundle.getSerializable("info");
        mdata = starBean.getStarinfo();
        //共享参数 保存选择的星座
        star_pref = getContext().getSharedPreferences("star_pref", Context.MODE_PRIVATE);

    }

    @Override
    public void onPause() {
        super.onPause();
        StarBean.StarinfoBean bean1 = mdata.get(posi);
        String name = bean1.getName();
        String logoname = bean1.getLogoname();
        //获取向共享参数中写入数据的对象
        SharedPreferences.Editor edit = star_pref.edit();
        edit.putString("name",name);
        edit.putString("logoname",logoname);
        edit.commit();
    }
}
